package com.topone.controller;

import com.topone.Enum.DynamicInfoEnum;
import com.topone.Exception.FanZoneException;
import com.topone.Form.DynamicForm;
import com.topone.Utils.KeyUntil;
import com.topone.Utils.ResultVOUntil;
import com.topone.VO.ResultVO;
import com.topone.dataObject.DynamicInfo;
import com.topone.dto.DynamicDTO;
import com.topone.service.DynamicInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created by Administrator on 2018/1/21,0021.
 */

@RestController
@CrossOrigin
@RequestMapping("/buyer/dynamic")
@Slf4j
public class dynamicController {

    @Autowired
    private DynamicInfoService service;

    @GetMapping("/list")
    public ResultVO<Map<String, Object>> list(@RequestParam(value = "bussinessId", required = false) String bussinessId,
                                              Map<String, Object> map) {

        try {
            DynamicDTO dynamicDTO = new DynamicDTO();
            if (StringUtils.isNotEmpty(bussinessId)) {
                dynamicDTO.setBussinessId(bussinessId);
            }
            dynamicDTO.setStatus(DynamicInfoEnum.success.getCode());
            List<DynamicDTO> dynamicInfoList = service.dynamicInfoList(dynamicDTO);
            if (Objects.isNull(dynamicInfoList) || dynamicInfoList.size() <= 0) {
                log.debug("根据商家ID,查询不到动态信息");
                return ResultVOUntil.error(1, "根据商家ID，查询不到动态信息");
            }
            map.put("dynamicInfoList", dynamicInfoList);
        } catch (FanZoneException e) {
            log.error("【查询商家动态信息】失败 e={}", e.getMessage());
            map.put("error", e.getMessage());
            return ResultVOUntil.error(1, e.getMessage());
        }
        map.put("success", "查询商家动态信息成功");
        log.info("【查询商家动态信息】成功:{}",map);
        return ResultVOUntil.success(map);
    }

    @GetMapping("/findOne")
    public ResultVO<Map<String, Object>> findDynamicByBusinessId(@RequestParam("bussinessId") String bussinessId,
                                                                 Map<String, Object> map) {

        try {
            DynamicDTO dynamicDTO = new DynamicDTO();
            dynamicDTO.setBussinessId(bussinessId);
            dynamicDTO.setStatus(DynamicInfoEnum.success.getCode());
            List<DynamicDTO> dynamicInfoList = service.findDynamicByBussiness(dynamicDTO);
            if (Objects.isNull(dynamicInfoList) || dynamicInfoList.size() <= 0) {
                log.debug("根据商家ID,查询不到动态信息");
                return ResultVOUntil.error(1, "根据商家ID，查询不到动态信息");
            }
            map.put("dynamicInfoList", dynamicInfoList);
        } catch (FanZoneException e) {
            log.error("【根据商家Id 查询商家动态信息】失败 e={}", e.getMessage());
            map.put("error", e.getMessage());
            return ResultVOUntil.error(1, e.getMessage());
        }
        map.put("success", "根据商家Id 查询商家动态信息成功");
        log.info("【根据商家Id 查询商家动态信息】成功：{}", map);
        return ResultVOUntil.success(map);
    }

    @GetMapping("/off_sale")
    public ResultVO<Map<String, Object>> offSale(@RequestParam("dynamicId") String dynamicId) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotEmpty(dynamicId)) {
            DynamicInfo dynamicInfo = service.findone(DynamicInfoEnum.success.getCode(), dynamicId);
            if (Objects.nonNull(dynamicInfo)) {
                dynamicInfo.setStatus(DynamicInfoEnum.error.getCode());
                service.save(dynamicInfo);
                map.put("success", "商家动态信息下架成功");
            } else {
                log.info("商家动态信息不存在或者状态不正确,{}", dynamicId);
                return ResultVOUntil.error(1, "动态信息不存在或者状态不正确");
            }
        }
        return ResultVOUntil.success(map);
    }

    @PostMapping("/save")
    public ResultVO save(@RequestBody DynamicForm dynamicForm) {

        if (Objects.nonNull(dynamicForm)) {
            DynamicInfo dynamicInfo = new DynamicInfo();
            BeanUtils.copyProperties(dynamicForm, dynamicInfo);
            dynamicInfo.setDynamicId(KeyUntil.getUniqueKey());
            dynamicInfo.setStatus(DynamicInfoEnum.success.getCode());
            try {
                service.save(dynamicInfo);
            } catch (FanZoneException e) {
                log.debug("商家动态信息 提交失败:{}", e.fillInStackTrace());
                return ResultVOUntil.error(1, "商家动态信息 提交失败：" + e.fillInStackTrace());
            }
            log.info("商家动态信息提交成功");
            return ResultVOUntil.success("商家动态信息 提交成功");
        } else {
            log.info("商家动态信息 无提交数据");
            return ResultVOUntil.error(1, "商家动态信息 无提交数据");
        }

    }

}
